package com.baiduspring.jdbc;

import com.google.common.base.Preconditions;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;

@Data
public class TemplateBean {
    /**
     * 返回字段
     */
    private Map<Integer, String> returnFields;
    /**
     * key：条件字段，对象数据库中表字段
     * value：条件查询对应的值
     */
    private Map<String, Object> conditionFields;
    /**
     * 返回对象的class
     */
    private Class returnObjClass;
    /**
     * 表名
     */
    private String tableName;

    public void validate() {
        Preconditions.checkArgument(!CollectionUtils.isEmpty(this.getReturnFields()), "返回字段为必填项");
        Preconditions.checkArgument(Objects.nonNull(this.returnObjClass), "返回类型为必填项");
        Preconditions.checkArgument(StringUtils.isEmpty(this.tableName), "表名为必填项");

        if (!CollectionUtils.isEmpty(this.conditionFields)) {
            this.conditionFields.entrySet().forEach(entity -> {
                Preconditions.checkArgument(StringUtils.isEmpty(entity.getKey()), "key为必填项");
                Preconditions.checkArgument(Objects.isNull(entity.getValue()), "value为必填项");
            });
        }
    }
}
